package com.youease.chris.view.mediator
{
	import com.youease.chris.control.CreateTextureCommand;
	import com.youease.chris.manager.GlobalObject;
	import com.youease.chris.manager.TexturesManager;
	import com.youease.chris.model.vo.MapData;
	import com.youease.chris.model.vo.TextureData;
	import com.youease.chris.utils.Common;
	import com.youease.chris.view.CreateTextureTitleWindow;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.FileListEvent;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class CreateTextureMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "CreateTextureMediator";
		public static var INDEX:int = 0;
		
		private var mapData:MapData;
		
		private var textures:ArrayCollection = new ArrayCollection();
		
		private var file:File;
		
		private var files:Vector.<File> = new Vector.<File>();
		
		public function CreateTextureMediator(viewComponent:Object)
		{
			super(NAME+INDEX, viewComponent);
			createTexture.select.addEventListener(MouseEvent.CLICK,onSelect);
			createTexture.create.addEventListener(MouseEvent.CLICK,onCreateTexture);
			INDEX++;
		}
		
		public function onCreateTexture(evt:MouseEvent):void {
//			mapData = new MapData(createTexture.mapName.text);
//			mapData.type = createTexture.mapType.selectedValue as int;
//			mapData.mapRows = createTexture.mapRows.value;
//			mapData.mapCols = createTexture.mapCols.value;
//			mapData.tileWidth = createTexture.tileWidth.value;
//			mapData.tileHeight = createTexture.tileHeight.value;
			
			sendNotification(CreateTextureCommand.CREATE_TEXTURE);
		}
		
		public function onSelect(evt:MouseEvent):void {
			var ff:FileFilter = new FileFilter("images","*.jpg;*.gif;*.png;*.bmp,*.jpeg");
			file = new File();
			file.browseForOpenMultiple("选择图片",[ff]);
			file.addEventListener(FileListEvent.SELECT_MULTIPLE,selectHandler);
		}
		
		public function selectHandler(evt:FileListEvent):void {
			file.removeEventListener(FileListEvent.SELECT_MULTIPLE,selectHandler);
			var files:Array = evt.files;
			for each(var f:File in files) {
//				loadFile(f);
				this.files.push(f);
			}
			loadFile();
		}
		public function loadFile():void {
			if(files.length>0) {
				file = files.pop();
				file.addEventListener(Event.COMPLETE,onFileLoadComplete);
				file.load();
			}
		}
		
		public function onFileLoadComplete(evt:Event):void {
			file = evt.target as File;
			var loader:Loader = new Loader();
			loader.unload();
			loader.loadBytes(file.data);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, image_completeHandler(file.name));
			
			writeImage(file.name,file.data);
		}
		
		public function image_completeHandler(fileName:String):Function {
			var func:Function = function(evt:Event):void {
				
				var td:TextureData = new TextureData();
				td.type = -1;
				td.img = file.name;
				var imgName:String = td.img.substring(0,td.img.lastIndexOf("."));
				
				td.type = GlobalObject.texturesType[imgName.substring(0,imgName.length-2)];
				td.index = int(imgName.substring(imgName.length-2));
				td.bitmapdata = Bitmap(evt.currentTarget.content).bitmapData;
				
				var exist:Boolean = false;
				for each(var t:TextureData in textures) {
					if(t.img == td.img) {
						exist = true;
						break;
					}
				}
				if(!exist) {
					textures.addItem(td);
					TexturesManager.textures[td.img] = td;
				}
				createTexture.textures.dataProvider = textures;
				loadFile();
			}
			return func;
		}
		
		public function writeImage(name:String,bytes:ByteArray):void {
			var fs:FileStream = new FileStream();
			file.nativePath = GlobalObject.projectDir+"/textures/"+name;
			trace(file.nativePath);
			fs.open(file,FileMode.WRITE);
			fs.writeBytes(bytes);
			fs.close();
		}
		
		override public function listNotificationInterests():Array {
			return [
				CreateTextureCommand.CREATE_TEXTURE_COMPLETE
			];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case CreateTextureCommand.CREATE_TEXTURE_COMPLETE:
					createTexture.closeWindow();
					break;
			}
		}
		
		public function get createTexture():CreateTextureTitleWindow {
			return this.viewComponent as CreateTextureTitleWindow;
		}
	}
}